package b4.view
{
	import as3isolib.display.IsoSprite;
	import as3isolib.geom.Pt;
	
	import b4.model.TownProxy;
	import b4.model.vo.player.Role;
	import b4.model.vo.player.Town;
	import b4.view.component.events.TownSceneEvent;
	import b4.view.component.town.PathFinding;
	import b4.view.component.town.TownScene;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;

	public class TownSceneMediator extends Mediator implements IMediator
	{
		public static const NAME:String = 'TownSceneMediator';
		public static const SET_EDIT_MODE:String = NAME + 'SetEditMode';
		public static const ADD_CHILD:String = NAME + 'AddChild';
		public static const REMOVE_CHILD:String = NAME + 'RemoveChild';
		public static const LOAD_TOWN_LAYER:String = NAME + 'LoadTownLayer';
		
		public function TownSceneMediator(viewComponent:Object=null)
		{
			super(NAME, viewComponent);
		}
		
		override public function listNotificationInterests():Array
		{
			return [
				TownSceneMediator.LOAD_TOWN_LAYER,
				TownSceneMediator.SET_EDIT_MODE,
				TownSceneMediator.ADD_CHILD,
				TownSceneMediator.REMOVE_CHILD,
//				RoleSpriteMediator.POSITION_CHANGE
			];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			var name:String = notification.getName();
			var body:Object = notification.getBody();
			var type:String = notification.getType();
			
			switch ( name )
			{
				case TownSceneMediator.LOAD_TOWN_LAYER:
					sendNotification(RoleSpriteMediator.REMOVE);
					townScene.refresh(body.townIndex, body.layerIndex);
					var town:Town = townProxy.vo.town;
					for (var roleIdStr:String in town.roleList)
					{
						var role:Role = town.getRoleById(parseInt(roleIdStr));
						if (role != null && role.layerId == town.layerId)
						{
							if (!facade.hasMediator(RoleSpriteMediator.NAME + role.id))
								facade.registerMediator(new RoleSpriteMediator(role.id));
							sendNotification(RoleSpriteMediator.SET_POS, {roleId: role.id, posX: role.posX, posY: role.posY});
							sendNotification(RoleSpriteMediator.SET_DEST, {roleId: role.id, posX: role.destX, posY: role.destY});
						}
					}
				break;
				case TownSceneMediator.SET_EDIT_MODE:
					townScene.editMode = Boolean(body);
				break;
				case TownSceneMediator.ADD_CHILD:
					var isoSprite:IsoSprite = body as IsoSprite;
					townScene.addChildToScene(isoSprite);
				break;
				case TownSceneMediator.REMOVE_CHILD:
					isoSprite = body as IsoSprite;
					townScene.removeChildFromScene(isoSprite);
				break;
//				case RoleSpriteMediator.POSITION_CHANGE:
//					var pos:Pt = IsoMath.isoToScreen(body as Pt, true);
//					var viewCenter:Pt = townScene.viewCenter;
//					if (Math.abs(viewCenter.x - pos.x) > 100 || Math.abs(viewCenter.y - pos.y) > 100)
//					{
//						townScene.viewCenter = pos;
//					} 
//				break;
			}
		}
		
		private function onSceneMouseClick(event:TownSceneEvent):void
		{
			var pt:Pt = event.data as Pt;
			if (!townScene.editMode)
			{
				var myRole:Role = townProxy.vo.town.myRole;
				if (myRole != null)
				{ 
					var roleMediator:RoleSpriteMediator = facade.retrieveMediator(RoleSpriteMediator.NAME + myRole.id) as RoleSpriteMediator;
					if (roleMediator != null)
						townProxy.setRoleDestination(roleMediator.roleSprite.x, roleMediator.roleSprite.y, pt.x, pt.y);
				}
//				var layerConfig:LayerConfig = townScene.getCurrentLayerConfig();
//				if (layerConfig != null)
//				{
//					sendNotification(RoleSpriteMediator.SET_DESTINATION, {layer:layerConfig, destPos:pt});
//				}
			}
			pt = PathFinding.posToSTD(pt);
			sendNotification(PaintDialogMediator.SHOW_INFO, pt);
		}
		
		override public function onRegister():void
		{
			sendNotification(ApplicationMediator.ADD_CHILD, viewComponent);
			townScene.addEventListener(TownSceneEvent.SCENE_MOUSE_CLICK, onSceneMouseClick);
		}
		
		override public function onRemove():void
		{
			townScene.close();
		}
		
		private function get townScene():TownScene
		{
			return viewComponent as TownScene;
		}
		
		private function get townProxy():TownProxy
		{
			return facade.retrieveProxy( TownProxy.NAME ) as TownProxy;
		}
	}
}